import java.util.Scanner;

/**
 * 主程序类，负责用户交互和流程控制
 */
public class SalaryManagementApp {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        SalaryManagementSystem system = new SalaryManagementSystem();
        system.loadEmployees();

        // 首次运行时初始化默认员工数据
        if (system.isEmployeesEmpty()) {
            initDefaultEmployees(system);
        }

        // 执行登录流程
        Employee loggedInEmp = loginProcess(scanner, system);
        if (loggedInEmp == null) {
            System.out.println("登录失败次数过多，程序退出。");
            scanner.close();
            return;
        }

        // 登录成功后显示功能菜单
        showMainMenu(scanner, system, loggedInEmp);

        scanner.close();
    }

    // 登录流程
    private static Employee loginProcess(Scanner scanner, SalaryManagementSystem system) {
        Employee loggedInEmp = null;
        int maxAttempts = 3; // 最大尝试次数
        int attempts = 0;

        while (attempts < maxAttempts) {
            System.out.println("\n===== 员工登录 =====");
            System.out.print("请输入账号：");
            String username = scanner.nextLine();
            System.out.print("请输入密码：");
            String password = scanner.nextLine();

            try {
                loggedInEmp = system.login(username, password);
                System.out.println("登录成功！欢迎您，" + loggedInEmp.getName() + "！");
                break;
            } catch (LoginFailedException e) {
                attempts++;
                System.out.println("登录失败：" + e.getMessage() + "，剩余尝试次数：" + (maxAttempts - attempts));
            }
        }
        return loggedInEmp;
    }

    // 主菜单（功能入口）
    private static void showMainMenu(Scanner scanner, SalaryManagementSystem system, Employee loggedInEmp) {
        while (true) {
            System.out.println("\n===== 系统菜单 =====");
            System.out.println("1. 查看我的工资条");
            System.out.println("2. 修改登录密码");
            System.out.println("3. 修改登录账号");
            System.out.println("4. 退出系统");
            System.out.print("请选择功能（1-4）：");
            String choice = scanner.nextLine();

            switch (choice) {
                case "1":
                    // 查看工资条
                    System.out.println("\n===== 您的工资条信息 =====");
                    System.out.println(system.getPersonalPaySlip(loggedInEmp));
                    break;
                case "2":
                    // 修改密码
                    changePasswordMenu(scanner, system, loggedInEmp);
                    break;
                case "3":
                    // 修改账号
                    changeUsernameMenu(scanner, system, loggedInEmp);
                    break;
                case "4":
                    // 退出系统
                    System.out.println("感谢使用，再见！");
                    return;
                default:
                    System.out.println("无效选项，请重新选择");
            }
        }
    }

    // 修改密码交互
    private static void changePasswordMenu(Scanner scanner, SalaryManagementSystem system, Employee loggedInEmp) {
        try {
            System.out.print("请输入原密码：");
            String oldPwd = scanner.nextLine();
            System.out.print("请输入新密码：");
            String newPwd = scanner.nextLine();
            system.changePassword(loggedInEmp, oldPwd, newPwd);
            System.out.println("密码修改成功！下次请使用新密码登录");
        } catch (Exception e) {
            System.out.println("修改失败：" + e.getMessage());
        }
    }

    // 修改账号交互
    private static void changeUsernameMenu(Scanner scanner, SalaryManagementSystem system, Employee loggedInEmp) {
        try {
            System.out.print("请输入新账号：");
            String newUsername = scanner.nextLine();
            system.changeUsername(loggedInEmp, newUsername);
            System.out.println("账号修改成功！下次请使用新账号登录");
        } catch (Exception e) {
            System.out.println("修改失败：" + e.getMessage());
        }
    }

    // 初始化默认员工数据（首次运行时调用）
    private static void initDefaultEmployees(SalaryManagementSystem system) {
        // 普通员工（账号：emp+工号，密码：123456）
        system.addEmployee(new RegularEmployee("cqpw", "E001", "empE001", "123456", 50, 160));
        system.addEmployee(new RegularEmployee("liiiiiiib", "E002", "empE002", "123456", 45, 150));
        system.addEmployee(new RegularEmployee("数学天才", "E003", "empE003", "123456", 60, 200));

        // 管理人员（账号：mgr+工号，密码：654321）
        system.addEmployee(new Manager("黎夕旧梦", "M001", "mgrM001", "654321", 20000, 5000));
        system.addEmployee(new Manager("小鞠之花", "M002", "mgrM002", "654321", 25000, 8000));
    }
}